home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
TeX 1995 July
/
TeX CD-ROM July 1995 (Disc 1)(Walnut Creek)(1995).ISO
/
web
/
noweb
/
src
/
c
/
finduses.c
< prev
next >
Wrap
C/C++ Source or Header
|
1995-02-24
|
4KB
|
132 lines
#line 4 "finduses.nw"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include "errors.h"
#include "match.h"
#include "getline.h"
#include "recognize.h"
#line 18 "finduses.nw"
static Recognizer nwindex;
#define ALPHANUM "_'@ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789#"
#define SYMBOLS "!%^&*-+:=|~<>./?`"
/* note $ and \ both delimiters */
#line 26 "finduses.nw"
static int showquotes = 1;
#line 90 "finduses.nw"
typedef struct line_and_outfile {
char *line;
FILE *out;
} LineOut;
#line 61 "finduses.nw"
static void read_ids(FILE *in);
#line 95 "finduses.nw"
static void add_use_markers(FILE *in, FILE *out);
#line 148 "finduses.nw"
static void write_index_use(void *closure, char *id, char *instance);
static char *emit_up_to(FILE *f, char *s, char *limit);
#line 30 "finduses.nw"
main(int argc, char **argv) {
FILE *fp;
char *myself = *argv;
int i;
for (i = 1; i < argc && argv[i][0] == '-' && argv[i][1] != 0; i++)
if (!strcmp(argv[i], "-noquote"))
showquotes = 0;
else
errormsg(Error, "%s: unknown option -%c\n", myself, argv[i][1]);
nwindex = new_recognizer(ALPHANUM, SYMBOLS);
if (i == argc) {
#line 72 "finduses.nw"
{ FILE *tmp = tmpfile();
char *line;
if (tmp == NULL)
#line 151 "finduses.nw"
errormsg(Fatal, "%s: couldn't open temporary file\n");
#line 75 "finduses.nw"
while ((line = getline(stdin)) != NULL) {
if (fputs(line, tmp) == EOF)
#line 153 "finduses.nw"
errormsg(Fatal, "%s: error writing temporary file\n");
#line 77 "finduses.nw"
if (is_index(line, "defn")) {
if (line[strlen(line)-1] == '\n') line[strlen(line)-1] = 0;
add_ident(nwindex, line+1+5+1+4+1);
} else if (is_index(line, "localdefn")) {
if (line[strlen(line)-1] == '\n') line[strlen(line)-1] = 0;
add_ident(nwindex, line+1+5+1+9+1);
}
}
rewind(tmp);
stop_adding(nwindex);
add_use_markers(tmp, stdout);
}
#line 43 "finduses.nw"
} else {
#line 53 "finduses.nw"
for (; i < argc; i++)
if ((fp=fopen(argv[i],"r"))==NULL)
errormsg(Error, "%s: couldn't open file %s\n", myself, argv[i]);
else {
read_ids(fp);
fclose(fp);
}
#line 45 "finduses.nw"
stop_adding(nwindex);
add_use_markers(stdin, stdout);
}
exit(errorlevel);
return errorlevel; /* slay warning */
}
#line 63 "finduses.nw"
static void read_ids(FILE *in) {
char *line;
while ((line = getline(in)) != NULL) {
if (line[strlen(line)-1] == '\n') line[strlen(line)-1] = 0;
add_ident(nwindex, line);
}
}
#line 97 "finduses.nw"
static void add_use_markers(FILE *in, FILE *out) {
char *line;
int incode = 0;
LineOut info; info.line = (char *)0; info.out = out;
while ((line = getline(in)) != NULL) {
if (is_begin(line, "code") || showquotes && is_keyword(line, "quote"))
incode = 1;
else if (is_end(line, "code") || is_keyword(line, "endquote"))
incode = 0;
if (is_keyword(line, "text") && incode) {
info.line = line + 6; /* skip "@text " */
search_for_ident(nwindex, line, write_index_use, &info);
if (*info.line && *info.line != '\n')
fprintf(out, "@text %s", info.line); /* has newline */
} else
fprintf(out, "%s", line);
}
}
#line 128 "finduses.nw"
static void write_index_use(void *closure, char *id, char *instance) {
LineOut *info = (LineOut *) closure;
info->line = emit_up_to(info->out, info->line, instance);
fprintf(info->out, "@index use %s\n", id);
info->line = emit_up_to(info->out, info->line, instance + strlen(id));
}
#line 136 "finduses.nw"
static char *emit_up_to(FILE *f, char *s, char *limit) {
if (s < limit) {
char saved = *limit;
*limit = 0;
fprintf(f, "@text %s\n", s);
*limit = saved;
return limit;
} else {
return s;
}
}